package com.jams.goodox.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.jams.goodox.R;


/**
 * @author sunday
 *         2013-12-04
 */
public class EditTextWithDel extends AppCompatEditText {
    private Drawable imgAble;
    private Context mContext;
    private boolean isNotShowDelete = false;
    private InEditAddDate inEditAddDate;

    public EditTextWithDel(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initAttrs(context,attrs);
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initAttrs(context,attrs);
        init();
    }
    /**
     * 获取xml属性设置
     * @param context
     * @param attrs
     */
    private void  initAttrs(Context context,AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithDel);
        try {
            if (typedArray != null) {
                imgAble=typedArray.getDrawable(R.styleable.EditTextWithDel_delete_icon);
            }
        } finally {
            if(typedArray != null)
            typedArray.recycle();
        }
        if(imgAble==null)imgAble = ContextCompat.getDrawable(mContext, R.mipmap.icon_del);
    }

    private void init() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isNotShowDelete)
                    return;
                if (inEditAddDate != null)
                    inEditAddDate.setOnchageListener(EditTextWithDel.this);//加入接口
                setDrawable();
            }
        });
        setDrawable();
    }

    public void setDeleteImg(Drawable drawable){
        if(drawable==null)return;
        this.imgAble=drawable;
    }

    public void addTextChangeListener(InEditAddDate addDate) {
        this.inEditAddDate = addDate;
    }

    //设置删除图片
    private void setDrawable() {
        if (length() < 1)
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        else
            setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
    }

    public void setNotShowDrawable() {
        isNotShowDelete = true;
    }

    // 处理删除事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Rect rect = new Rect();
            getGlobalVisibleRect(rect);
            rect.left = rect.right - 100;
            if (rect.contains(eventX, eventY))
                setText("");
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    /**
     * 目的是用来处理多个编辑框编辑问题
     */
    public interface InEditAddDate {
        void setOnchageListener(EditTextWithDel editText);
    }

}
